package NWPackage;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;


public class CandidatesDistMatrix {
	
	private HashMap<String,RankDist> candidates;

	public CandidatesDistMatrix()
	{
		this.candidates = new HashMap<String, RankDist>();
	}
	
	public CandidatesDistMatrix(HashMap<String, RankDist> otherCandidates) {
		this.candidates = new HashMap<String, RankDist>();
		for (String cand : otherCandidates.keySet()) {
			this.candidates.put(cand, otherCandidates.get(cand).clone());
		}
	}

	public void calcRangeVotesProbs(List<ProbabilityCell> agentProbs) {
		String[] candidatesAvailable = agentProbs.get(0).getCandidates();
		
		for (String candidate : candidatesAvailable) {
			RankDist rankList = new RankDist();
			
			for (int vote = 1; vote <= candidatesAvailable.length; vote++) {
				double voteProb = calcVoteProb(vote,candidate,agentProbs);
				rankList.addRank(vote, voteProb);
			}
			
			this.candidates.put(candidate, rankList);
		}
		
	}

	private double calcVoteProb(int vote, String candidate,List<ProbabilityCell> agentProbs) {
		double prob = 0;
		for (ProbabilityCell cell : agentProbs) {
			if (cell.getIndexOf(candidate) == vote-1)
				prob +=cell.getProbability();
		}
		return prob;
	}

	public double getVoteProb(String candidate, int vote) {
		return this.candidates.get(candidate).getVoteProb(vote);
	}

	public void initial(String[] cands, int numOfVotes) {
		for (String cand : cands) {
			RankDist rankList = new RankDist();
			for (int i = 1; i <= numOfVotes; i++) {
				rankList.addRank(i, 0);
			}
			this.candidates.put(cand, rankList);
		}
		
	}

	public int getNumberOfVotes(String candidate) {
		return this.candidates.get(candidate).getNumOfVotes();
	}

	public void fixRanks(String candidate, int i) {
		this.candidates.get(candidate).fixRanks(i);
		
	}

	public void initial(int numCandidates, int numOfVotes) {
		for (int cand = 1; cand <= numCandidates; cand++) {
			RankDist rankList = new RankDist();
			for (int j = 1; j <= numOfVotes; j++) {
				rankList.addRank(j, 0);
			}
			this.candidates.put(String.valueOf(cand), rankList);
		}
		
	}

	public void add(String candidate, List<String> votes) {
		RankDist rankList = new RankDist();
		for (int i = 1; i <= votes.size(); i++) {
			rankList.addRank(i, Double.parseDouble(votes.get(i-1)));
		}
		this.candidates.put(candidate,rankList);
		
	}

	public List<String> getCandidatesList() {
		return new ArrayList<String>(candidates.keySet());
	}

	public RankDist getAllRankDist(String candidate) {
		return this.candidates.get(candidate);
	}

	public void add(HashMap<String, RankDist> allProbsIsWinner) {
		for (String cand : allProbsIsWinner.keySet()) {
			this.candidates.get(cand).replaceRankDistWith(allProbsIsWinner.get(cand));
		}
	}
	public CandidatesDistMatrix clone()
	{
		CandidatesDistMatrix candMatrix = new CandidatesDistMatrix(this.candidates);
		return candMatrix;
	}
	
}
